package scantask.server.domainLayer;

import java.util.Set;
import java.util.Vector;

import scantask.server.dataLayer.DataManipulatorInterface;
import scantask.server.dataLayer.SQLManipulator;
import scantask.server.domainLayer.Exceptions.AddBenchmarkException;
import scantask.server.domainLayer.Exceptions.GetInformativeDataException;
import scantask.server.domainLayer.Exceptions.GetUserBenchmarksException;
import scantask.server.domainLayer.Exceptions.GetValidDatesException;
import scantask.server.domainLayer.Exceptions.LoginException;

import scantask.containers.Area;
import scantask.containers.Benchmark;
import scantask.containers.Coordinate;
import scantask.containers.InformationObject;
import scantask.containers.User;
import scantask.containers.WeekDate;
import scantask.containers.exceptions.UnInitializeObjectException;

public class Controller implements ControllerInterface {

	private UserController _userController;
	private DataController _dataController;
	
	public Controller() {
		
		DataManipulatorInterface dataManipulator = new SQLManipulator();
		
		this._userController = new UserController(dataManipulator);
		this._dataController = new DataController(dataManipulator);
		
	}
	
	
	@Override
	public void addBenchmark(String userName, String locationName, Coordinate coordinate) throws AddBenchmarkException {
		if (this._userController.isLoggedIn(userName)) {
			this._dataController.addBenchmark(userName, locationName, coordinate);
		}
		else {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("add benchmark failed " + userName + " is not login");
			throw new AddBenchmarkException("Error - Add benchmark failed, User " + userName + " is not login");
		}
	}

/* already get it in the login	
	@Override
	public Coordinate getDefaultLocation(String userName) throws GetDefaultLocationException {
		if (this._userController.isLoggedIn(userName)) {
			return this._dataController.getDefaultLocation(userName);
		}
		else {
			server.utilities.logging.SingletonLogger.getLogger().warning("Get user default location failed " + userName + " is not login");
			throw new GetDefaultLocationException("Error - Get default location failed, User " + userName + " is not login");
		}
	}
*/
	
	@Override
	public Vector<InformationObject> getInformativeData(String userName, WeekDate date) throws GetInformativeDataException {
		if (this._userController.isLoggedIn(userName)) {
			Vector<Area> areas = null;
			try {
				areas = this._userController.getUserAreas(userName);
			} catch (LoginException e) {
				// AdirD - Cannot reach this point
				scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Get informative data failed " + userName + " is not login");
				throw new GetInformativeDataException("Error - Get informative data failed, User " + userName + " is not login");
			} catch (UnInitializeObjectException e) {
				scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Error - Get informative data failed, User " + userName + " area is null pointer");
				throw new GetInformativeDataException("Error - Get informative data failed, User " + userName + " area is null pointer");
			}
			return this._dataController.getInformativeData(userName, date, areas);
		}
		else {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Get informative data failed " + userName + " is not login");
			throw new GetInformativeDataException("Error - Get informative data failed, User " + userName + " is not login");
		}
	}

	@Override
	public Vector<Benchmark> getUserBenchmarks(String userName) throws GetUserBenchmarksException {
		if (this._userController.isLoggedIn(userName)) {
			return this._dataController.getUserBenchmarks(userName);
		}
		else {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Get user benchmarks failed " + userName + " is not login");
			throw new GetUserBenchmarksException("Error - Get user benchmarks failed, User " + userName + " is not login");
		}
	}

	@Override
	public Set<WeekDate> getValidDates(String userName) throws GetValidDatesException {
		if (this._userController.isLoggedIn(userName)) {
			return this._dataController.getValidDates(userName);
		}
		else {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Get valid dates failed " + userName + " is not login");
			throw new GetValidDatesException("Error - Get valid dates failed, User " + userName + " is not login");
		}
	}

	@Override
	public User login(String userName, String password) throws LoginException {
		return this._userController.login(userName, password);
	}

	@Override
	public void logout(String userName) {
		this._userController.logout(userName);
	}


/* already get it in the login	
	@Override
	public String getUserFullName(String userName) {
		return this._userController.getUserFullName(userName);
	}
	*/

}
